package com.ttdevs.ttrss.db;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ttdevs.ttrss.bean.ArticleCnbeta;
import com.ttdevs.ttrss.db.DBHelper.TableArticle;
import com.ttdevs.ttrss.db.DBHelper.TableCnbeta;

public class DBCnbetaManager implements IDBCnbetaManager {
	private static IDBCnbetaManager dbm;
	private static SQLiteDatabase db;

	private DBCnbetaManager(Context context) {
		db = new DBHelper(context).getWritableDatabase();
	}

	public static IDBCnbetaManager getInstance(Context context) {
		if (dbm == null) {
			dbm = new DBCnbetaManager(context);
		}
		return dbm;
	}

	@Override
	public boolean insertArticles(List<ArticleCnbeta> articles) {
		boolean isSuccess = false;
		db.beginTransaction();
		try {
			for (ArticleCnbeta article : articles) {
				ContentValues cv = new ContentValues();
				cv.put(TableCnbeta.TITLE, article.getTitle());
				cv.put(TableCnbeta.PUBTIME, article.getPubtime());
				cv.put(TableCnbeta.ARTICLEID, article.getArticleID());
				cv.put(TableCnbeta.CMTCLOSED, article.isCmtClosed() ? 1 : 0);
				cv.put(TableCnbeta.CMTNUM, article.getCmtNum());
				cv.put(TableCnbeta.SUMMARY, article.getSummary());
				cv.put(TableCnbeta.TOPICLOGO, article.getTopicLogo());
				cv.put(TableCnbeta.THEME, article.getTheme());
				cv.put(TableCnbeta.MARK, 0); // 0未标记，1标记
				cv.put(TableCnbeta.READ, 0); // 0未读，1已读
				cv.put(TableCnbeta.CREATETIME, System.currentTimeMillis());

				db.insert(TableCnbeta.TABLE_NAME, null, cv);
			}
			db.setTransactionSuccessful();
			isSuccess = true;
		} catch (Exception e) {
			isSuccess = false;
		} finally {
			db.endTransaction();
		}
		return isSuccess;
	}

	@Override
	public boolean markArticle(int articleID, boolean isMark) {
		// ContentValues cv = new ContentValues();
		// cv.put(TableArticle.MARK, isMark ? 1 : 0);
		// String whereClause = TableArticle._ID + " = ? ";
		// String[] whereArgs = new String[] { String.valueOf(articleID) };
		// db.update(TableArticle.TABLE_NAME, cv, whereClause, whereArgs);
		return false;
	}

	@Override
	public boolean markArticleRead(int articleID, boolean isRead) {
		// ContentValues cv = new ContentValues();
		// cv.put(TableArticle.READ, isRead ? 1 : 0);
		// String whereClause = TableArticle._ID + " = ? ";
		// String[] whereArgs = new String[] { String.valueOf(articleID) };
		// db.update(TableArticle.TABLE_NAME, cv, whereClause, whereArgs);
		// return false;
		return false;
	}

	@Override
	public boolean deleteArticles() {
		// String whereClause = TableArticle.LINK + " = ? ";
		// String[] whereArgs = new String[] { url };
		// db.delete(TableArticle.TABLE_NAME, whereClause, whereArgs);
		// return false;
		return false;
	}

	@Override
	public List<ArticleCnbeta> queryArticle(int articleID, int pageSize) {
		List<ArticleCnbeta> list = new ArrayList<ArticleCnbeta>();

		// String selection = TableArticle.SOURCE + " = ? ";
		// String[] selectionArgs = new String[] { source };
		Cursor cursor = db.query(TableCnbeta.TABLE_NAME, null, null, null, null, null, null);
		if (cursor.getCount() <= 0) {
			return list;
		}
		while (cursor.moveToNext()) {
			ArticleCnbeta article = new ArticleCnbeta();
			article.setTitle(cursor.getString(cursor.getColumnIndex(TableArticle.TITLE)));
			list.add(article);
		}
		return list;
	}
}
